package com.fanhao.dllo.justforyou.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.FoodHouseAdapter;
import com.fanhao.dllo.justforyou.entity.FoodHouseListResult;
import com.fanhao.dllo.justforyou.view.SpacesItemDecoration;
import com.google.gson.Gson;

import java.util.List;

public class FoodHouseActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private FoodHouseAdapter foodHouseAdapter;
    private ImageView backIv;

    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_house);


        initView();
        initData();


    }
    private void initView() {
        // 初始化recyclerView并绑定布局管理器
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

        GridLayoutManager gm = new GridLayoutManager(this,2);
                gm.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(gm);
        recyclerView.addItemDecoration(new SpacesItemDecoration(15));
        // 绑定适配器
        foodHouseAdapter = new FoodHouseAdapter(this);

        recyclerView.setAdapter(foodHouseAdapter);
        backIv = (ImageView)findViewById(R.id.backIv);
        backIv.setOnClickListener(this);


    }

    private void initData() {
        queue = Volley.newRequestQueue(this);
        String url ="http://112.80.255.88/naserver/search/commonitem?compId=catg&logpage=CateList&locate_city_id=1300010000&hot_bizarea_id=-1&page_idx=0&bizarea_list=0&sort_type=0&snd_cattag_id=880&page_type=component&goods_per_page=15&compV=1.4.5&fst_cattag_id=326&cityid=1300010000&appid=android&tn=android&terminal_type=android&device=YuLong+Coolpad+8675-FHD&channel=bainuo&v=6.0.1&os=SDK19&sdkversion=1.0&location=38.889847%2C121.551034&cuid=04D245A971F5A6AE19141F6507D49342%7C494550320493668&uuid=00000000-2caf-bb74-e03f-9d5a58e881e0&bduss=Wx-S2d1THktaFJZT1lWYlBDRXVJTU0yZklUTEgtR1o2aH5iRVhwSWdIeVZEd2xXQVFBQUFBJCQAAAAAAAAAAAEAAABmXqktYmFiec7SsK7D3ruozMcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJWC4VWVguFVQ&timestamp=1447149647927&swidth=1080&sheight=1920&net=wifi&appkey=sFTRwpfNpZihllpqhpionpCirKCjy1mRh9Sgl6SapqaiVJuDldCbqaOanFKQV6mj0ZislKqEcYfT2aOjmVa0&packname=com.nuomi&platform=android&sign=a2755b45764de632c99218e493c78f5d";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Gson gson = new Gson();
                        FoodHouseListResult flr = gson.fromJson(response,
                                FoodHouseListResult.class);

                        if (flr != null) {
                            FoodHouseListResult.DataEntity dataEntity = flr.getData();
                            if (dataEntity != null) {
                                List<FoodHouseListResult.DataEntity.Poi_list> poiList = dataEntity.getPoi_list();
                                foodHouseAdapter.addData(poiList);
                            }

                        }
//                        List<FoodHouseListData> datas = flr.getData();
//                        List<FoodHouseListPoi> foodHouseListPoi = new ArrayList<>();
//                        for (FoodHouseListData foodHouseListData : datas) {
//                            FoodHouseListPoi poi = foodHouseListData.getPoi();
//                            foodHouseListPoi.add(poi);
//                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                       error.printStackTrace();
                    }
                });

        queue.add(request);
    }


    @Override
    public void onClick(View v) {
        finish();
    }
}
